import {
    cardformInitData,
    queryCardFormButtonsWithUserId,
    getDictDatasAll,
    internation,
    queryCardFormColumnsWithUserId
} from '@/api/api'

export default {
    async created() {
        await this._getDictDatasAll()
        await this._cardformInitData()
        await this._internation()
        this._queryCardFormButtonsWithUserId()
    },
    methods: {
        // 查询字段权限
        async _queryCardFormColumnsWithUserId() {
            queryCardFormColumnsWithUserId(this.formId)
                .then((res) => {
                    if (res) {
                        console.log(res)
                        res.forEach((item) => {
                            const temp = {}
                            const data = item.formEntityTablename.replace(/_/g, '') + '_' + item.formFieldTablename.toUpperCase()
                            if (item.formColumnType === '1' && this.dictColumn.hasOwnProperty(data)) {
                                temp.data = data
                                temp.type = this.getFormColumnGuiType(item.formColumnGuiType)
                                if (temp.type === 'date') {
                                    temp.dateFormat = 'YYYY-MM-DD'
                                }
                                if (temp.type === 'dropdown') {
                                    temp.source = []
                                    console.log(this.dictColumn[data][0])
                                    console.log(this.dictColumn[data][0].columnTypeDetail.dictTypeCode)
                                    const arr = this.dictValues[this.dictColumn[data][0].columnTypeDetail.dictTypeCode]
                                    arr.forEach(item => {
                                        temp.source.push(item.name)
                                    })
                                }
                                // dropdown
                                temp.readOnly = item.formColumnShowType !== 'edit'
                                if (this.columnObj[data]) {
                                    this.rowColumn.push(this.columnObj[data])
                                    this.columns.push(temp)
                                    this.colHeaders.push(this.columnObj[data].columnName)
                                }
                            }
                        })
                        // console.info(this.rowColumn)
                    }
                })
        },

        // 国际化
        _internation() {
            internation(this.formId)
                .then(res => {
                    console.log(res)
                    const local = res.data
                    console.log(local)
                    this.$i18n.mergeLocaleMessage('en', local.en)
                    this.$i18n.mergeLocaleMessage('zh', local['zh-CN'])
                })
        },

        _getDictDatasAll() {
            getDictDatasAll(this.formId)
                .then(res => {
                    console.log(res)
                    this.dictValues = res
                    this._queryCardFormColumnsWithUserId()
                })
        },
        _cardformInitData(heightGrade) {
            // console.log(this.pager)
            var params = {
                'order': 'asc',
                'offset': (this.pager.offset - 1) * this.pager.limit,
                'limit': this.pager.limit || 20,
                'containerParam': {},
                'paramsMap': {},
                'requestParam': {
                    'ORGVORGANIZATION_DEPCODE': ''
                },
                'filter': this.filterData,
                'userIds': null,
                'formId': this.formId,
                'isView': null,
                'heightGrade': heightGrade || []
            }
            cardformInitData(params)
                .then((res) => {
                    console.log(res)
                    this.totalData = res.total
                    const data = res.rows
                    data.forEach(item => {
                        for (const key in item) {
                            if (item.hasOwnProperty(key) && key.indexOf('NEWDATE') !== -1) {
                                const tempName = key.replace('_NEWDATE', '')
                                item[tempName] = item[key]
                            }
                        }
                    })
                    this.trueTableDate = data
                    console.log(data)
                    this.listLoading = false
                })
        },
        // 查询按钮权限
        _queryCardFormButtonsWithUserId() {
            queryCardFormButtonsWithUserId(this.formId)
                .then((res) => {
                    if (res) {
                        const temp = {}
                        res.forEach(item => {
                            temp[item.formButtonCode] = item.powerType
                        })
                        this.buttonValid = temp
                    }
                })
        }
    }
}
